<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/26 0026
 * Time: 14:47
 */

namespace app\admin\logic;

use app\admin\model\Origin as Origin_model;
use function PHPSTORM_META\type;
use think\Request;
use think\Cache;
use nx\Data;

class Origin
{

    public $Origin_model = '';

    /**获取来源模型
     * @return Origin_model|string
     */
    public function getModel()
    {
        if (empty($this->Origin_model)) {
            $this->Origin_model = new Origin_model();
        }
        return $this->Origin_model;
    }

    /**获取所有的来源（供获取器使用）
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public static function OriginsToText()
    {
        $Origins = self::getAllOrigins();
        $Origins = collect($Origins)->column('title', 'id');
        return $Origins;
    }

    /**获取所有的来源来源（供获取器使用）
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public static function OriginsToInt(){
        $Origins = self::getAllOrigins();
        $Origins = collect($Origins)->column('id','title');
        return $Origins;
    }

    /**将所有来源进行树形排列
     * @return array|false|mixed|\PDOStatement|string|\think\Collection
     */
    public function getAllOriginToTree()
    {
        $Origins = $this->getAllOrigins();
        $Origins = Data::tree($Origins, 'title', 'id');
        return $Origins;
    }

    /**获得所有的来源（从缓存中）
     * @return false|mixed|\PDOStatement|string|\think\Collection   返回数组
     */
    public function getAllOrigins()
    {
        $Origins = Cache::get('allOrigins');
        if (!empty($Origins)) return $Origins;
        $Origins = $this->getModel()->where('status', 1)->order('sort')->select();
        if (empty($Origins)) return $Origins;
        $Origins = collection($Origins)->append(['status_text'])->toArray();
        Cache::set('allOrigins', $Origins);
        return $Origins;
    }


    /**
     * 获取来源 select 数据
     * @return array
     * @author: Huang
     */
    public function getAllText()
    {
        $Origins = $this->getAllOrigins();
        $companys = collect($Origins)->where('status', 1)->all();
        return $companys;
    }

    /**
     * 获取某来源的所有下属类别
     * @param $Origin_id
     * @return array|mixed
     */
    public function getNextOrigins($Origin_id)
    {
        $data = $this->getAllOrigins();
        $Origins = Data::channelList($data, $Origin_id, '— ', 'id');
        return $Origins;
    }


    public function addOrigin($data)
    {
        if (empty($data)) {
            return ['result' => false, 'msg' => '数据丢失'];
        }
        if (!empty($data['code'])) {
            $data['code'] = strtoupper(trim($data['code']));
        }
        $result = $this->getModel()->allowField(true)->validate(true)->save($data);
        if (false === $result) {
            return ['result' => false, 'msg' => $this->getModel()->getError()];
        } else {
            Cache::rm('allOrigins');
            return ['result' => true, 'msg' => '来源添加成功'];
        }
    }

    public function editOrigin($data)
    {
        if (empty($data['id'])) {
            return ['result' => false, 'msg' => 'ID丢失'];
        }
        isset($data['status']) ?: $data['status'] = 0;
        $result = $this->getModel()->allowField(true)->validate(true)->isUpdate(true)->save($data);
        if (false === $result) {
            return ['result' => false, 'msg' => $this->getModel()->getError()];
        } else {
            Cache::rm('allOrigins');
            return ['result' => true, 'msg' => '来源修改成功'];
        }
    }

    public function delOrigin($id)
    {
        if (empty($id)) {
            return ['result' => false, 'msg' => '请选择要操作的数据'];
        }
        if (Origin_model::get($id)) {
            if (Origin_model::destroy($id)) {
                Cache::rm('allOrigins');
                return ['result' => true, 'msg' => '删除成功'];
            } else {
                return ['result' => false, 'msg' => '删除失败'];
            }
        } else {
            return ['result' => false, 'msg' => '要删除的数据不存在'];
        }
    }
}